package com.whfc.openapi.api.fse.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.fse.dto.OpenApiFseLiftDTO;
import com.whfc.fse.dto.OpenApiFseLiftDataDTO;
import com.whfc.fse.dto.OpenApiFseLiftRecordDTO;
import com.whfc.fse.param.FseLiftDataAddParam;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.api.fse.service.OpenApiFseLiftService;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClasssName FseLiftController
 * @Description 升降机开放接口
 * @Author hw
 * @Date 2021/2/4 16:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/v1/fse/lift")
public class FseLiftController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private OpenApiFseLiftService fseLiftService;

    @GetMapping("/list")
    public Result list(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取升降机列表,deptId:{}", deptId);
        ListData<OpenApiFseLiftDTO> pageData = fseLiftService.list(deptId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/device/info")
    public Result deviceInfo(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                             @RequestParam(value = "sn") String sn) {
        logger.info("开放平台获取升降机实时数据,sn:{}", sn);
        OpenApiFseLiftDataDTO data = fseLiftService.deviceInfo(sn);
        return ResultUtil.success(data);
    }

    @GetMapping("/data/real")
    public Result dataReal(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                           @RequestParam(value = "sn") String sn,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime") Date startTime,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime") Date endTime) {
        logger.info("开放平台获取升降机历史数据,sn:{}，startTime：{}，endTime：{}", sn, startTime, endTime);
        ListData<?> data = fseLiftService.dataReal(sn, startTime, endTime);
        return ResultUtil.success(data);
    }


    @GetMapping("/data/record")
    public Result dataRecord(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                             @RequestParam(value = "sn") String sn,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime") Date startTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime") Date endTime) {
        logger.info("开放平台获取升降机工作记录,sn:{}，startTime：{}，endTime：{}", sn, startTime, endTime);
        ListData<OpenApiFseLiftRecordDTO> data = fseLiftService.dataRecord(sn, startTime, endTime);
        return ResultUtil.success(data);
    }

    @PostMapping("/data/add")
    public Result dataAdd(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestBody @Validated FseLiftDataAddParam param) {
        logger.info("开放平台新增升降机实时数据,param：{}", param);
        fseLiftService.dataAdd(param);
        return ResultUtil.success();
    }


    /**
     * 探越升降机实时数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/realData")
    public Result tanyueRealData(@RequestBody String data) {
        logger.info("探越升降机实时数据,param：{}", data);
        fseLiftService.tanyueRealData(data);
        return ResultUtil.success();
    }

    /**
     * 探越升降机报警数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/warnData")
    public Result tanyueWarnData(@RequestBody String data) {
        logger.info("探越升降机报警数据,param：{}", data);
        fseLiftService.tanyueWarnData(data);
        return ResultUtil.success();
    }

    /**
     * 探越升降机循环工作数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/workData")
    public Result tanyueWorkData(@RequestBody String data) {
        logger.info("探越升降机循环工作数据,param：{}", data);
        fseLiftService.tanyueWorkData(data);
        return ResultUtil.success();
    }
}
